package com.maomao.annotation.field;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/4/19.
 */
public class Test {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        Person person = new Person();

        Field f1 = person.getClass().getDeclaredField("name");
        System.out.println(f1);
        Field[] f2 = person.getClass().getDeclaredFields();
        for (Field f:f2) {
            System.out.println(f);
        }
        Field f3 = person.getClass().getField("name");
        System.out.println(f3);
        Field[] f4 = person.getClass().getFields();
        for (Field f:f4) {
            System.out.println(f);
        }
        person.setName("xiaomin");
        person.setAge(100);
        person.setSex("men");
        System.out.println(f1.get(person));
        f3.set(person,"wenwen");
        for (Field f:f2) {

            f.setAccessible(true);
            System.out.println(f.get(person));
        }
        List<Field> fList = new ArrayList<Field>();
        Map<String,Field> map = new HashMap<>();
        for (Field f:f2){
            if (f.isAnnotationPresent(KeyName.class)){
                KeyName keyName =f.getAnnotation(KeyName.class);
                map.put(keyName.name(),f);


            }
        }
       for (Map.Entry entry:map.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
       }
        Class<?> a=Person.class;
        Person p = (Person) a.newInstance();
        p.setName("xiao");
        System.out.println(p.getName());

    }
}
